<?php
class Rstobi_Model_BknRireki extends Apm_Model_Base{

	//strHistFlg:閲覧履歴存在フラグ
	//strOemCd:OEMCD(OEM先から送られてくる。店舗コードまたは企業コード)
	//strUseKbn:利用ｻｲﾄ区分(既存は)

	public function getRirekihyoujiData($strOemCd,$strUseKbn){
		// テスト
		//setcookie("AP_HIST_CNT",4);
		//setcookie("AP_HIST",10,time() - 3600);
		//setcookie("AP_HIST1_Bukken1","20100601002891");
		//setcookie("AP_HIST1_Bukken2","20100629178102");
		//setcookie("AP_HIST1_Bukken3","20080821011510");
		//setcookie("AP_HIST1_Bukken4","20100923001820");
		//setcookie("AP_HIST_CNT",6);
		//setcookie("AP_HIST1_Bukken6","20050705217914");

		//var_dump($_COOKIE);
		try{
			$strQry;
			$getSortStr = " ORDER BY (CASE WHEN T03_HEYA_CD IS NOT NULL THEN 1 ELSE 0 END) DESC,T82_SEQ_NO DESC, T03_TOUROKUBI DESC ";

			$db_params = array();
			if($strOemCd == "" AND $strUseKbn == "0"){
				if($_COOKIE[Apm_Config_Rstdtl::NAME_COOKIE_RIREKI_CNT] <> ""){
					$strCondition = $this->getDecodeWhere(Apm_Config_Rstdtl::NAME_COOKIE_RIREKI, Apm_Config_Rstdtl::NAME_COOKIE_RIREKI_CNT);
					//$strQry = $this->getSqlLite($strCondition[0], $strCondition[1]) + $this->getSortStr("22", "1");
					if (empty($strCondition)) return array();
					$strQry = $this->getSqlRireki($strCondition[0], $strCondition[1]).$getSortStr;
					$db_params = $strCondition[2];
					
					//クエリ実行
					return $this->dbGetList($strQry,$db_params);
				}
			}
			return array();
		}
		catch (Exception $e)
		{
			// エラーメッセージをログに書き出す
			__log_debug(__FILE__, __LINE__, __METHOD__, $e->getMessage());

			// 外部モデル呼び出し設定
			$loader = new Zend_Application_Module_Autoloader(array(
				'namespace' => 'Rstdtl',
				'basePath' => APPLICATION_PATH . '/modules/rstdtl',
			));
			// モデル生成
			$this->_model_dtlcookie = new Rstdtl_Model_Dtlcookie();

			// クッキー削除
			$this->_model_dtlcookie->DeleteAllCookie(Apm_Config_Rstdtl::NAME_COOKIE_RIREKI, Apm_Config_Rstdtl::NAME_COOKIE_RIREKI_CNT);

			return array();
		}
	}

	// お気に入り物件を取得
	public function getOkiniirihyoujiData($userId = "") {

		try{
			// 非ログイン時：クッキー情報を取得
			if ($userId == "") {
				if($_COOKIE[Apm_Config_Rstdtl::NAME_COOKIE_OKINIIRI_CNT] <> "") {
					$strCondition = $this->getDecodeWhere(Apm_Config_Rstdtl::NAME_COOKIE_OKINIIRI, Apm_Config_Rstdtl::NAME_COOKIE_OKINIIRI_CNT);
					if (empty($strCondition)) return array();
				}
				else {
					// お気に入り登録なし
					return array();
				}
			}
			else {
				// ログイン時
				$strCondition[0] = "";
				$strCondition[1] = "";
				$strCondition[2] = array();
			}

			// SQL生成
			$db_params = array();
			$db_params = $strCondition[2];
			$sql = $this->getSelOkiniiri($userId, $strCondition[0], $strCondition[1], $db_params);

			// クエリ実行
			return $this->dbGetList($sql, $db_params);
		}
		catch(Exception $e)
		{
			// エラーメッセージをログに書き出す
			__log_debug(__FILE__, __LINE__, __METHOD__, $e->getMessage());

			if ($userId == "")
			{
				// 外部モデル呼び出し設定
				$loader = new Zend_Application_Module_Autoloader(array(
					'namespace' => 'Rstdtl',
					'basePath' => APPLICATION_PATH . '/modules/rstdtl',
				));
				// モデル生成
				$this->_model_dtlcookie = new Rstdtl_Model_Dtlcookie();

				// クッキー削除
				$this->_model_dtlcookie->DeleteAllCookie(Apm_Config_Rstdtl::NAME_COOKIE_OKINIIRI,Apm_Config_Rstdtl::NAME_COOKIE_OKINIIRI_CNT);
			}

			return array();
		}
	}

	public function getDecodeWhere($strName,$strCntName){
		$j=0;
		$strWhere = "";
		$strDecode = "";
		$strCookieName;
		$arrLite;
		$db_params = array();
		$arrSQLPARTS = Array();		//(0)=DECODE文(1)=WHERE句(2)=db_params
		
		// 外部モデル呼び出し設定
		$loader = new Zend_Application_Module_Autoloader(array(
			'namespace' => 'Rstdtl',
			'basePath' => APPLICATION_PATH . '/modules/rstdtl',
		));
		// モデル生成
		$this->_model_dtlcookie = new Rstdtl_Model_Dtlcookie();

		$count = $_COOKIE[$strCntName];
		$loop_end = max($count-4, 0);
		$strDecode = " DECODE(T03_HEYA_CD,";
		$strWhere = " T03_HEYA_CD IN ( ";

		// Cookieを連想配列で取得
		$cookieArray = $this->_model_dtlcookie->getCookieAll($strName, $count);
		if (empty($cookieArray)) {
			// 取得できなければ、処理をスルー
			return null;
		}

		// 降順ソート
		krsort($cookieArray);

		$index = 0;
		foreach($cookieArray as $key => $value){

			if($j <> 0){
				$strWhere .= " , ";
			}
			$valueStr = "value_" . $index;
			$keyStr = "key_" . $index;
			$db_params[$valueStr] = $value;
			$db_params[$keyStr] = str_replace("Bukken","",$key);
			
			$strWhere .= ":" . $valueStr;	//部屋CD
			$strDecode .= " ' :". $valueStr."', :".$keyStr.",";
			// インクリメント
			$j++;
			$index++;
		}

		$strDecode .= "'0' ) as T82_SEQ_NO ";
		$strWhere .= " ) ";

		$arrSQLPARTS[0] = $strDecode;
		$arrSQLPARTS[1] = $strWhere;
		$arrSQLPARTS[2] = $db_params;

		return $arrSQLPARTS;
	}



	//============================================================================''
	//	引数
	//		Id	- Cookie格納物件No
	//		CName - Cookie名称
	//	戻り値
	//		Cookie名称
	//============================================================================''
	public function getCookieName($Id,$CName){
		$cookieName = "";
		if($Id < 21){
			$cookieName =$CName."1";
		}else{
			$cookieName =$CName."2";
		}
		return $cookieName;
	}

	//============================================================================''
	//	説明
	//		Lite用SQL作成
	//============================================================================''
	//	引数
	//		strDecode
	//		strWhere
	//============================================================================''
	public function getSqlRireki($strDecode, $strWhere){

	//$strNewDate = "";
	//新着基準日の算出(=1週間前)
	//$date_now = date('Y/m/d');
	//$strNewDate = DateAdd( "d", -7, Date() );
			$strSQL = $this->getSqlSelect();

			//DECODE文挿入
			if($strDecode <> ""){
				$strSQL .= ",".$strDecode;
			}

			$strSQL .= "FROM T03WEB_HEYA ";
			//WHERE句挿入
			if($strWhere <> ""){
				$strSQL .= "WHERE ".$strWhere;
			}

//print_r($strSQL);
		return	$strSQL;
	}

	/**
	 * SELECT句取得
	 *
	 * @access  public
	 * @params  ユーザID
	 * @params  Decode文
	 * @params  Where文
	 * @return  String
	 */
	public function getSelOkiniiri($userId, $strDecode, $strCookieWhere, &$db_params)
	{
		$strSelect = $this->getSqlSelect();
		$strFrom   = " FROM T03WEB_HEYA ";
		$strWhere  = " WHERE ";
		$strOrder  = " ORDER BY (CASE WHEN T03_HEYA_CD IS NOT NULL THEN 1 ELSE 0 END) DESC, T82_SEQ_NO DESC, T03_TOUROKUBI DESC ";

		if (!empty($userId))
		{
			// ログインユーザ
			//  SELECT追加
			$strSelect .= " ,T99.T82_SEQ_NO ";
			//  FROM追加
			$strFrom .= ",(";
			$strFrom .= "    SELECT ";
			$strFrom .= "         T99_USER_CD ";
			$strFrom .= "        ,T99_HEYA_CD ";
			$strFrom .= "        ,T99_TOUROKUBI as T82_SEQ_NO ";
			$strFrom .= "        ,row_number() over (order by T99_TOUROKUBI desc, T99_HEYA_CD) as NUM ";
			$strFrom .= "    FROM ";
			$strFrom .= "        T99FAVORITE ";
			$strFrom .= "    WHERE ";
			$strFrom .= "        exists (";
			$strFrom .= "            SELECT 'x' ";
			$strFrom .= "            FROM M92MYPAGE_USER ";
			$strFrom .= "            WHERE ";
			$strFrom .= "                M92_E_MAIL = :userId ";
			$strFrom .= "                AND M92_DELETE_FLG = '0' ";
			$strFrom .= "                AND M92_USER_CD = T99_USER_CD ";
			$strFrom .= "        ) ";
			$strFrom .= "        AND exists (";
			$strFrom .= "            SELECT 'x' ";
			$strFrom .= "            FROM T03WEB_HEYA ";
			$strFrom .= "            WHERE T99_HEYA_CD = T03_HEYA_CD ";
			$strFrom .= "        ) ";
			$strFrom .= " ) T99 ";
			//  WHERE追加
			$strWhere .= " T99.T99_HEYA_CD = T03WEB_HEYA.T03_HEYA_CD ";
			$strWhere .= " AND T99.NUM <= 4 ";

			// パラメータセット
			$db_params['userId'] = $userId;
		}
		else
		{
			// 非ログインユーザ
			//  SELECT追加
			$strSelect .= ", ".$strDecode;
			//  WHERE追加
			$strWhere .= $strCookieWhere;
		}

		return $strSelect . $strFrom . $strWhere . $strOrder;
	}

	/**
	 * SELECT句取得
	 *
	 * @access  public
	 * @return  String
	 */
	public function getSqlSelect()
	{
		$strSelect = <<< DOC_END
SELECT
     T03_HEYA_CD
    ,T03_TATEMONO_CD
    ,T03_TENPO_CD
    ,T03_TENPO_NAME
    ,T03_ORIGINAL_TATEMONO_CD01
    ,T03_ORIGINAL_TATEMONO_CD02
    ,T03_ORIGINAL_HEYA_CD01
    ,T03_ORIGINAL_HEYA_CD02
    ,T03_TATEMONO_NAME
    ,T03_TATEMONO_NAME_KOUKAI
    ,T03_YUUBIN
    ,T03_TODOUFUKEN_CD
    ,T03_SHIKUCHOUSON_CD
    ,T03_CHOUMEI_CD
    ,T03_CHOUMOKU_CD
    ,T03_BANCHI
    ,T03_JYUUSHO_TEXT
    ,T03_ANNAI_MAP_USE_FLAG
    ,T03_TATEMONO_TYPE_CD
    ,T03_TATEMONO_TYPE
    ,T03_TATEMONO_KOUZOU_CD
    ,T03_TATEMONO_KOUZOU
    ,T03_KAIDATE
    ,T03_KOSUU
    ,T03_KANSEINEN
    ,T03_KANSEITSUKI
    ,T03_KANSEIBI
    ,T03_SHINCHIKU_FLAG
    ,T03_TATEMONO_MENSEKI
    ,T03_SHIKICHI_MENSEKI
    ,T03_SENYUU_MIN
    ,T03_SENYUU_MAX
    ,T03_HEYA_BANGOU
    ,T03_HEYA_BANGOU_HYOUZI
    ,T03_KAI
    ,T03_HOUGAKU
    ,T03_MADORI_CD
    ,T03_MADORI
    ,T03_HEYA_KAZU
    ,T03_MADORI_SHOUSAI_CD01
    ,T03_MADORI_SHOUSAI_CD02
    ,T03_MADORI_SHOUSAI_CD03
    ,T03_MADORI_SHOUSAI_CD04
    ,T03_MADORI_SHOUSAI_CD05
    ,T03_MADORI_SHOUSAI_CD06
    ,T03_MADORI_SHOUSAI_CD07
    ,T03_MADORI_SHOUSAI_CD08
    ,T03_MADORI_SHOUSAI_CD09
    ,T03_MADORI_SHOUSAI_CD10
    ,T03_MADORI_SHOUSAI01
    ,T03_MADORI_SHOUSAI02
    ,T03_MADORI_SHOUSAI03
    ,T03_MADORI_SHOUSAI04
    ,T03_MADORI_SHOUSAI05
    ,T03_MADORI_SHOUSAI06
    ,T03_MADORI_SHOUSAI07
    ,T03_MADORI_SHOUSAI08
    ,T03_MADORI_SHOUSAI09
    ,T03_MADORI_SHOUSAI10
    ,T03_MADORI_MENSEKI01
    ,T03_MADORI_MENSEKI02
    ,T03_MADORI_MENSEKI03
    ,T03_MADORI_MENSEKI04
    ,T03_MADORI_MENSEKI05
    ,T03_EKI_NAME
    ,T03_ENSEN_NAME
    ,T03_ENSEN_CD01
    ,T03_TOHO01
    ,T03_BUS_JYOUSHA
    ,T03_BUS_STOP
    ,T03_KYOUEKIHI
    ,T03_KYOUEKIHI_TAX
    ,T03_KANRIHI
    ,T03_KANRIHI_TAX
    ,T03_YACHIN
    ,T03_YACHIN_TAX
    ,T03_SHIKIKIN_TSUKISUU
    ,T03_SHIKIKIN
    ,T03_REIKIN_TSUKISUU
    ,T03_REIKIN
    ,T03_HOSHOUKIN_TSUKISUU
    ,T03_HOSHOUKIN
    ,T03_SHOUKYAKUKIN_TSUKISUU
    ,T03_SHOUKYAKUKIN
    ,T03_SHOUKYAKUKINRITSU
    ,T03_SENYUU_MENSEKI
    ,T03_GAIKAN_GAZOU01
    ,T03_GAIKAN_GAZOU02
    ,T03_GAIKAN_GAZOU03
    ,T03_GAIKAN_GAZOU04
    ,T03_GAIKAN_GAZOU05
    ,T03_GAIKAN_GAZOU_COMMON01
    ,T03_GAIKAN_GAZOU_COMMON02
    ,T03_GAIKAN_GAZOU_COMMON03
    ,T03_GAIKAN_GAZOU_COMMON04
    ,T03_GAIKAN_GAZOU_COMMON05
    ,T03_FREE_GAZOU01
    ,T03_FREE_GAZOU_TYPE01
    ,T03_FREE_GAZOU02
    ,T03_FREE_GAZOU_TYPE02
    ,T03_FREE_GAZOU03
    ,T03_FREE_GAZOU_TYPE03
    ,T03_FREE_GAZOU04
    ,T03_FREE_GAZOU_TYPE04
    ,T03_FREE_GAZOU05
    ,T03_FREE_GAZOU_TYPE05
    ,T03_FREE_GAZOU06
    ,T03_FREE_GAZOU_TYPE06
    ,T03_FREE_GAZOU07
    ,T03_FREE_GAZOU_TYPE07
    ,T03_FREE_GAZOU08
    ,T03_FREE_GAZOU_TYPE08
    ,T03_FREE_GAZOU09
    ,T03_FREE_GAZOU_TYPE09
    ,T03_FREE_GAZOU10
    ,T03_FREE_GAZOU_TYPE10
    ,T03_FREE_GAZOU11
    ,T03_FREE_GAZOU_TYPE11
    ,T03_FREE_GAZOU12
    ,T03_FREE_GAZOU_TYPE12
    ,T03_FREE_GAZOU_COMMON01
    ,T03_FREE_GAZOU_TYPE_COMMON01
    ,T03_FREE_GAZOU_COMMON02
    ,T03_FREE_GAZOU_TYPE_COMMON02
    ,T03_FREE_GAZOU_COMMON03
    ,T03_FREE_GAZOU_TYPE_COMMON03
    ,T03_FREE_GAZOU_COMMON04
    ,T03_FREE_GAZOU_TYPE_COMMON04
    ,T03_FREE_GAZOU_COMMON05
    ,T03_FREE_GAZOU_TYPE_COMMON05
    ,T03_FREE_GAZOU_COMMON06
    ,T03_FREE_GAZOU_TYPE_COMMON06
    ,T03_FREE_GAZOU_COMMON07
    ,T03_FREE_GAZOU_TYPE_COMMON07
    ,T03_FREE_GAZOU_COMMON08
    ,T03_FREE_GAZOU_TYPE_COMMON08
    ,T03_FREE_GAZOU_COMMON09
    ,T03_FREE_GAZOU_TYPE_COMMON09
    ,T03_FREE_GAZOU_COMMON10
    ,T03_FREE_GAZOU_TYPE_COMMON10
    ,T03_FREE_GAZOU_COMMON11
    ,T03_FREE_GAZOU_TYPE_COMMON11
    ,T03_FREE_GAZOU_COMMON12
    ,T03_FREE_GAZOU_TYPE_COMMON12
    ,T03_MADORIZU
    ,T03_KOUKOKU_KAISHA_CD
    ,T03_PANORAMA_GAZOU01
    ,T03_PANORAMA_GAZOU02
    ,T03_MADORIZU_3D
    ,T03_NAISOU_GAZOU01
    ,T03_NAISOU_GAZOU02
    ,DECODE( TRIM( NVL(T03_GAIKAN_GAZOU01, '') ), '', 1, 0 ) GAIKAN_UMU_FLAG
    ,DECODE( TRIM( NVL(T03_MADORIZU, '') ), '', 1, 0 ) MADORI_UMU_FLAG
    ,DECODE( TRIM( NVL(T03_PANORAMA_GAZOU01, '') ), '', 1, 0 ) PANORAMA1_UMU_FLAG
    ,DECODE( TRIM( NVL(T03_PANORAMA_GAZOU02, '') ), '', 1, 0 ) PANORAMA2_UMU_FLAG
    ,DECODE( TRIM( NVL(T03_MADORIZU_3D, '') ), '', 1, 0 ) MADORI_3D_UMU_FLAG
    ,CASE WHEN (( to_date(to_char(sysdate-7,'yyyymmdd'),'yyyymmdd') < T03_KOUSHINBI ) AND ( T03_KIGENGIRE_SHUUSEI_FLAG <> 1 )) THEN 0 ELSE 1 END AS SHINCHAKU_FLAG
    ,CASE WHEN ( (TRIM(NVL(T03_NAISOU_GAZOU01, ''))IS NOT NULL) OR (TRIM(NVL(T03_NAISOU_GAZOU02, '')) IS NOT NULL) ) THEN 0 ELSE 1 END AS NAISOU_UMU_FLAG
    ,T03_CAMPAIGN_FLAG
    ,T03_CAMPAIGN2_FLAG
    ,T03_CAMPAIGN3_FLAG
    ,T03_CAMPAIGN4_FLAG
    ,T03_CAMPAIGN5_FLAG
    ,T03_CAMPAIGN6_FLAG
    ,T03_CAMPAIGN7_FLAG
    ,T03_CAMPAIGN8_FLAG
    ,T03_CAMPAIGN9_FLAG
    ,T03_CAMPAIGN10_FLAG
    ,T03_KASHIDASHI_KUBUN
    ,T03_FREERENT_FLAG
    ,T03_HEYAWARI_FLAG
DOC_END;

		return $strSelect;
	}

  /**
    * 前回の閲覧履歴を表示用に変換
    *
    * @access    public
    * @param    array
    * @return    void
    */
    public function convertRirekiData($bukkenData)
    {
        $Rireki = array();

		for($i = 0; $i < count($bukkenData); $i++)
		{
			// 表示用コンバード
			$bukkenData[$i]['T03_HEYA_CD']            = $bukkenData[$i]['T03_HEYA_CD'];							// 部屋CD
			$bukkenData[$i]['T03_JUSYO_STR']          = Apm_Bukkenitems::GetAddressStr($bukkenData[$i]);		// 住所
			$bukkenData[$i]['T03_YACHIN_STR']         = Apm_Bukkenitems::GetChinryouStr($bukkenData[$i]);		// 賃料
			$bukkenData[$i]['T03_KANRI_KYOEKI_STR']   = Apm_Bukkenitems::GetKyouekihiStr($bukkenData[$i], '0');	// 管理費・共益費
			$bukkenData[$i]['T03_TATEMONO_TYPE_STR']  = Apm_Bukkenitems::GetBuildTypeStr($bukkenData[$i]);		// 建物種別
			$bukkenData[$i]['T03_MADORI_STR']         = Apm_Bukkenitems::GetMadoriStr($bukkenData[$i]);			// 間取り
			$bukkenData[$i]['T03_SENYUU_MENSEKI_STR'] = Apm_Bukkenitems::GetSenyuuMensekiStr($bukkenData[$i]);	// 専有面積
			$bukkenData[$i]['T03_BUILD_YM_STR']       = Apm_Bukkenitems::GetBuildYmStr($bukkenData[$i]);		// 築年月
			// 外観画像
			$images = Apm_Bukkenitems::GetImageDataMain($bukkenData[$i]);
			$bukkenData[$i]['T03_GAIKAN_GAZOU_ARR']   = $images[Apm_Config_Search::IMG_TYPE_GAIKAN][0];
			// URL
			$bukkenData[$i]['T03_BUKENDTL_URL']       = Apm_Bukkenurl::generateUrlByParametersDetail(array('ID'=>$bukkenData[$i]['T03_HEYA_CD']));
		}

		return $bukkenData;
    }
}
?>
